package com.aviccxzx.smarthome.utils;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;


public  class DialogUtil {

	private AlertDialog alertDialog;
	private LinearLayout layout;
	public static Context context;
	public SureInterfance listener;

	public interface SureInterfance{
		public void sureTodo();
		public void cancle();
	}
	/**
	 * 
	 * @param context
	 * @param l 是否显示左边按钮
	 * @param r 是否显示右边按钮
	 * @param AlertContent 提示内容
	 * @param LContent 左边按钮的内容
	 * @param RContent 右边按钮的内容
	 * @param listener 右边按钮的接口回调
	 */
	public DialogUtil(Context context, boolean l, boolean r, String AlertContent, String LContent, String RContent, final SureInterfance listener){
		layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.clear_cache_dialog, null);
		alertDialog = new AlertDialog.Builder(context).create();
		alertDialog.setCancelable(false);
		this.context = context;
		this.listener = listener;
		TextView textView_line=(TextView) layout.findViewById(R.id.textView_line);
		textView_line.setWidth(DensityUtil.dip2px(context, 3.5f));
		TextView txtContent = (TextView) layout.findViewById(R.id.txt_content);
		txtContent.setText(AlertContent);
		Button btnCancle = (Button)layout.findViewById(R.id.btn_cancle);
		btnCancle.setText(LContent);
		btnCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.cancle();
				if(alertDialog.isShowing())
					alertDialog.dismiss();
			}
		});
		Button btnSure  = (Button)layout.findViewById(R.id.btn_sure);
		btnSure.setText(RContent);
		btnSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				listener.sureTodo();
				alertDialog.dismiss();
			}
		});

		if(!l){
			btnSure.setBackgroundResource(R.drawable.clean_cache_dialog_right_single);
			btnCancle.setVisibility(View.GONE);
			textView_line.setVisibility(View.GONE);
		}
		if(!r){
			btnCancle.setBackgroundResource(R.drawable.clean_cache_dialog_btn_single);
			btnSure.setVisibility(View.GONE);
			textView_line.setVisibility(View.GONE);
		}
	}

	public DialogUtil(Context context, boolean l, boolean r, String AlertTitle, String AlertContent, String LContent, String RContent, final SureInterfance listener){
		layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.clear_cache_dialog, null);
		alertDialog = new AlertDialog.Builder(context).create();
		this.context = context;
		this.listener = listener;
		TextView textView_line=(TextView) layout.findViewById(R.id.textView_line);
//		textView_line.setWidth(DensityUtil.dip2px(context, 0.5f));
		TextView txtContent = (TextView) layout.findViewById(R.id.txt_content);
		TextView txtTitle = (TextView) layout.findViewById(R.id.txt_title);
		if (AlertContent!=null) {
			txtContent.setText(AlertContent);
		}

		if (AlertTitle!=null) {
			txtTitle.setText(AlertTitle);
		}
		Button btnCancle = (Button)layout.findViewById(R.id.btn_cancle);
		btnCancle.setText(LContent);
		btnCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(alertDialog.isShowing())
					alertDialog.dismiss();
			}
		});

		Button btnSure  = (Button)layout.findViewById(R.id.btn_sure);
		btnSure.setText(RContent);
		btnSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				listener.sureTodo();
				alertDialog.dismiss();
			}
		});

		if(!l){
			btnSure.setBackgroundResource(R.drawable.clean_cache_dialog_right_single);
			btnCancle.setVisibility(View.GONE);
			textView_line.setVisibility(View.GONE);
		}
		if(!r){
			btnCancle.setBackgroundResource(R.drawable.clean_cache_dialog_btn_single);
			btnSure.setVisibility(View.GONE);
			textView_line.setVisibility(View.GONE);
		}
	}

	public  void showCustomDialog() {
		alertDialog.show();

		alertDialog.setContentView(layout);
		Window window = alertDialog.getWindow();
		LayoutParams layoutParams = window.getAttributes();
		WindowManager manager =window.getWindowManager();
		Display display = manager.getDefaultDisplay();
		layoutParams.width = (int) (display.getWidth()*0.9);
		window.setAttributes((WindowManager.LayoutParams) layoutParams);
	}
	
	public void dismiss(){
		alertDialog.dismiss();
	}
	
	public void setOnDismissListener(DialogInterface.OnDismissListener listener){
		alertDialog.setOnDismissListener(listener);
	}





}
